<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="" prop="id">
				<el-input v-model="dataForm.id" placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="订单编号" prop="orderNumber">
				<el-input v-model="dataForm.orderNumber" placeholder="订单编号"></el-input>
			</el-form-item>
			<el-form-item label="发货地（国家）" prop="sendCountry">
				<el-input v-model="dataForm.sendCountry" placeholder="发货地（国家）"></el-input>
			</el-form-item>
			<el-form-item label="发货地(州)" prop="sendProvince">
				<el-input v-model="dataForm.sendProvince" placeholder="发货地(州)"></el-input>
			</el-form-item>
			<el-form-item label="发货地(城市)" prop="sendCity">
				<el-input v-model="dataForm.sendCity" placeholder="发货地(城市)"></el-input>
			</el-form-item>
			<el-form-item label="目的地(城市)" prop="goalCountry">
				<el-input v-model="dataForm.goalCountry" placeholder="目的地(城市)"></el-input>
			</el-form-item>
			<el-form-item label="目的地(州)" prop="goalProvince">
				<el-input v-model="dataForm.goalProvince" placeholder="目的地(州)"></el-input>
			</el-form-item>
			<el-form-item label="目的地(城市)" prop="goalCity">
				<el-input v-model="dataForm.goalCity" placeholder="目的地(城市)"></el-input>
			</el-form-item>
			<el-form-item label="发货时间" prop="sendTime">
				<el-input v-model="dataForm.sendTime" placeholder="发货时间"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="价格" prop="price">
				<el-input v-model="dataForm.price" placeholder="价格"></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="catalog">
				<el-input v-model="dataForm.catalog" placeholder="类型"></el-input>
			</el-form-item>
			<el-form-item label="单号状态 0-运输途中 1-收到信息 2-成功签收 3-可能异常" prop="orderStatus">
				<el-input v-model="dataForm.orderStatus" placeholder="单号状态 0-运输途中 1-收到信息 2-成功签收 3-可能异常"></el-input>
			</el-form-item>
			<el-form-item label="上传时间" prop="createTime">
				<el-input v-model="dataForm.createTime" placeholder="上传时间"></el-input>
			</el-form-item>
			<el-form-item label="购买时间" prop="updateTime">
				<el-input v-model="dataForm.updateTime" placeholder="购买时间"></el-input>
			</el-form-item>
			<el-form-item label="购买人id" prop="buyId">
				<el-input v-model="dataForm.buyId" placeholder="购买人id"></el-input>
			</el-form-item>
			<el-form-item label="上传供应商id" prop="sellId">
				<el-input v-model="dataForm.sellId" placeholder="上传供应商id"></el-input>
			</el-form-item>
			<el-form-item label="删除" prop="deleted">
				<el-input v-model="dataForm.deleted" placeholder="删除"></el-input>
			</el-form-item>
			<el-form-item label="创建人" prop="creator">
				<el-input v-model="dataForm.creator" placeholder="创建人"></el-input>
			</el-form-item>
			<el-form-item label="修改人" prop="updater">
				<el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
			</el-form-item>
			<el-form-item label="版本" prop="version">
				<el-input v-model="dataForm.version" placeholder="版本"></el-input>
			</el-form-item>
			<el-form-item label=" 购买状态 0-未购买 1-已经购买" prop="buyStatus">
				<el-input v-model="dataForm.buyStatus" placeholder=" 购买状态 0-未购买 1-已经购买"></el-input>
			</el-form-item>
			<el-form-item label="购买金额" prop="realPrice">
				<el-input v-model="dataForm.realPrice" placeholder="购买金额"></el-input>
			</el-form-item>
			<el-form-item label="金额" prop="balance">
				<el-input v-model="dataForm.balance" placeholder="金额"></el-input>
			</el-form-item>
			<el-form-item label="购买方式" prop="buyWay">
				<el-input v-model="dataForm.buyWay" placeholder="购买方式"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useSyswalletApi, useSyswalletSubmitApi } from '@/api/wallet/wallet'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	orderNumber: '',
	sendCountry: '',
	sendProvince: '',
	sendCity: '',
	goalCountry: '',
	goalProvince: '',
	goalCity: '',
	sendTime: '',
	remark: '',
	price: '',
	catalog: '',
	orderStatus: '',
	createTime: '',
	updateTime: '',
	buyId: '',
	sellId: '',
	deleted: '',
	creator: '',
	updater: '',
	version: '',
	buyStatus: '',
	realPrice: '',
	balance: '',
	buyWay: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getSys_user_wallet(id)
	}
}

const getSys_user_wallet = (id: number) => {
	useSyswalletApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useSyswalletSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
